/* Copyright (C) 2007-2008  klevgert  http://code.google.com/p/db4o-netbeans/

This file is part of the db4o-netbeans module suite.

db4o-netbeans is free software; you can redistribute it and/or modify it under
the terms of version 2 of the GNU General Public License as published
by the Free Software Foundation and as clarified by db4objects' GPL 
interpretation policy, available at
http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
Suite 350, San Mateo, CA 94403, USA.

db4o-netbeans is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. */

package com.db4o.nb.editor.nodes;

import com.db4o.nb.api.Constrain;
import com.db4o.nb.api.Db4oProvider;
import com.db4o.nb.editor.nodes.ConstrainNode.ConstrainNodeChildren;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import org.openide.loaders.DataObject;
import org.openide.nodes.AbstractNode;
import org.openide.nodes.Children;
import org.openide.nodes.Node;
import org.openide.util.NbBundle;

/**
 * A node that represents the visualization of a value constraint.
 *
 * @author klevgert
 */
public class ValueConstrainNode extends ConstrainNode {
    
  /** Creates a new instance of Db4oDescendNode */
  public ValueConstrainNode( Constrain constrain ) {
    super(constrain, new ConstrainNodeChildren(constrain));
    
    this.setDisplayName( constrain.getTag() + ": " + constrain.getValue());
  }
  
  /**
   * Gets the action that could be performed on the node.
   * @param b   flag indicating if the menu is a popup 
   * @return Array of actions to execute on the node
   */
  public Action[] getActions(boolean b) {

    Action[] superActions = super.getActions(b);
    return new Action[] { 
      new AddNotAction(),
      new AddGreaterAction(),
      new AddSmallerAction(),
      null,
      superActions[0]
    };
  }  
 
  public void reloadChildren() {
    this.setChildren(new ConstrainNodeChildren(this.constrain));
  }

  public void update() {
    this.reloadChildren();
  }
  
  // <editor-fold defaultstate="collapsed" desc=" Actions ">                          

   /**
   * Adds a not constrain
   */
  private class AddNotAction extends AbstractAction {
    
    public AddNotAction() {
      String displayName = NbBundle.getMessage(this.getClass(), "AddNot");
      this.putValue(NAME,displayName);
    }
    
    public void actionPerformed(ActionEvent e) {
      
      Node node = ValueConstrainNode.this;
      while( !(node instanceof QueryNode) ) {
        node = node.getParentNode();
      }
      QueryNode queryNode = (QueryNode)node;
      Db4oDataNode dbNode = (Db4oDataNode)queryNode.getParentNode().getParentNode();
//      Db4oDataNode dataNode = (Db4oDataNode)dbNode.getParentNode();
      DataObject dataObj = dbNode.getDataObject();
      Db4oProvider provider = (Db4oProvider)dataObj.getCookie(Db4oProvider.class);
      Constrain newConstrain = provider.createNot();
      
      constrain.add(newConstrain);
      dataObj.setModified(true);

      ValueConstrainNode.this.setChildren( new ConstrainNodeChildren(constrain));
    }
  }
  
   /**
   * Adds a greater constrain
   */
  private class AddGreaterAction extends AbstractAction {
    
    public AddGreaterAction() {
      String displayName = NbBundle.getMessage(this.getClass(), "AddGreater");
      this.putValue(NAME,displayName);
    }
    
    public void actionPerformed(ActionEvent e) {
      Node node = ValueConstrainNode.this;
      while( !(node instanceof QueryNode) ) {
        node = node.getParentNode();
      }
      QueryNode queryNode = (QueryNode)node;
      Db4oDataNode dbNode = (Db4oDataNode)queryNode.getParentNode().getParentNode();
 //     Db4oDataNode dataNode = (Db4oDataNode)dbNode.getParentNode();
      DataObject dataObj = dbNode.getDataObject();
      Db4oProvider provider = (Db4oProvider)dataObj.getCookie(Db4oProvider.class);
      Constrain newConstrain = provider.createGreater();
      
      constrain.getChildren().add(newConstrain);
      dataObj.setModified(true);

      ValueConstrainNode.this.setChildren( new ConstrainNodeChildren(constrain));
    }
  }
  
   /**
   * Adds a smaller constrain
   */
  private class AddSmallerAction extends AbstractAction {
    
    public AddSmallerAction() {

      String displayName = NbBundle.getMessage(this.getClass(), "AddSmaller");
      this.putValue(NAME,displayName);
    }
    
    public void actionPerformed(ActionEvent e) {
      Node node = ValueConstrainNode.this;
      while( !(node instanceof QueryNode) ) {
        node = node.getParentNode();
      }
      QueryNode queryNode = (QueryNode)node;
      Db4oDataNode dbNode = (Db4oDataNode)queryNode.getParentNode().getParentNode();
 //     Db4oDataNode dataNode = (Db4oDataNode)dbNode.getParentNode();
      DataObject dataObj = dbNode.getDataObject();
      Db4oProvider provider = (Db4oProvider)dataObj.getCookie(Db4oProvider.class);
      Constrain newConstrain = provider.createGreater();
      
      constrain.getChildren().add(newConstrain);
      dataObj.setModified(true);

      ValueConstrainNode.this.setChildren( new ConstrainNodeChildren(constrain));
    }
  }
       
  // </editor-fold>
  
}
